package com.bw.park

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bw.lib_base.entity.ParkBean
import com.bw.lib_base.entity.ParkBeanItem
import com.bw.lib_base.entity.Res
import com.bw.lib_base.net.ApiService

/**
 * para：请求网络的参数值
 * type：请求哪个接口
 */
class ParkDataSource(val apiService: ApiService,val para: Any,val type: String): PagingSource<Int, ParkBeanItem>() {

    override fun getRefreshKey(state: PagingState<Int, ParkBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ParkBeanItem> {
        try {
            var res:Res<ParkBean>?=null
            if (type == "类型"){
                res = apiService.getParkByType(para.toString())
            }else if (type == "状态"){
                res = apiService.getParkByStatus(para.toString())
            }else if (type == "区域"){
                res = apiService.getParkByArea(para.toString().toInt())
            }
            return LoadResult.Page(res?.data!!,null,null)
        }catch (e: Exception){
            return LoadResult.Error(e)
        }
    }
}